package com.asianfo.dao;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;

import com.asianfo.dao.meta.ColumnMetaData;
import com.asianfo.dao.meta.TableMetaData;

public class SqlStatementGen {

	public String genInsert(SqlExcutorContext sec) {
		TableMetaData tmd = sec.getTableMetaData();
		StringBuilder sqlBuilder = new StringBuilder("INSERT INTO `").append(tmd.getName()).append("` (");
		StringBuilder questionMarkBuilder = new StringBuilder();
		Collection<ColumnMetaData> cmds = tmd.getColumns();
		int i = 1;
		for (ColumnMetaData columnMetaData : cmds) {
			sec.addRequireParameter(columnMetaData.getName());
			sqlBuilder.append("`").append(columnMetaData.getName()).append("`");
			questionMarkBuilder.append("?");
			if (i != cmds.size()) {
				sqlBuilder.append(",");
				questionMarkBuilder.append(", ");
			}
			i++;
		}
		return sqlBuilder.append(") VALUES (").append(questionMarkBuilder).append(")").toString();
	}

	public String genUpdate(SqlExcutorContext sqlExcutorContext) {
		TableMetaData tmd = sqlExcutorContext.getTableMetaData();
		StringBuilder sqlBuilder = new StringBuilder("UPDATE FROM `").append(tmd.getName()).append("` SET ");
		Collection<ColumnMetaData> columnMetaDatas = tmd.getColumns();
		genSegmentByColumns(sqlExcutorContext, columnMetaDatas, sqlBuilder);
		genCondition(sqlExcutorContext, sqlBuilder);
		return sqlBuilder.toString();
	}

	private void genCondition(SqlExcutorContext sqlExcutorContext, StringBuilder sqlBuilder) {
		Collection<ColumnMetaData> columnMetaDatas = sqlExcutorContext.getConditionColumnMetaData();
		if (columnMetaDatas == null || columnMetaDatas.isEmpty())
			return;
		sqlBuilder.append(" WHERE ");
		genSegmentByColumns(sqlExcutorContext, columnMetaDatas, sqlBuilder);
	}

	public String genDalete(SqlExcutorContext sqlExcutorContext) {
		TableMetaData tmd = sqlExcutorContext.getTableMetaData();
		StringBuilder sqlBuilder = new StringBuilder("DELETE FROM `").append(tmd.getName()).append("`");
		genCondition(sqlExcutorContext, sqlBuilder);
		return sqlBuilder.toString();
	}

	public Object genSelect(SqlExcutorContext sqlExcutorContext) {
		TableMetaData tmd = sqlExcutorContext.getTableMetaData();
		StringBuilder sqlBuilder = new StringBuilder("SELECT * FROM `").append(tmd.getName()).append("`");
		genCondition(sqlExcutorContext, sqlBuilder);
		return sqlBuilder.toString();
	}

	private void genSegmentByColumns(SqlExcutorContext sqlExcutorContext, Collection<ColumnMetaData> columnMetaDatas, StringBuilder sqlBuilder) {
		int i = 1;
		for (ColumnMetaData columnMetaData : columnMetaDatas) {
			sqlExcutorContext.addRequireParameter(columnMetaData.getName());
			sqlBuilder.append("`").append(columnMetaData.getName()).append("`= ?");
			if (i != columnMetaDatas.size()) {
				sqlBuilder.append(", ");
			}
			i++;
		}
	}

	public static void main(String[] args) {

	}

	@Test
	public void testGenInsert() {
		System.out.println(genInsert(new SqlExcutorContext().setTableMetaData(new TableMetaData() {
			{
				setName("tb_name");
				setColumns(Arrays.asList(//
						new ColumnMetaData() {
					{
						setName("a");
					}

				}, new ColumnMetaData() {
					{
						setName("c");
					}

				}, new ColumnMetaData() {
					{
						setName("b");
					}

				}));
			}
		})));
	}

	@Test
	public void testGenUpdate() {
		System.out.println(genUpdate(new SqlExcutorContext() {
			{
				setConditionColumnMetaData(Arrays.asList(new ColumnMetaData() {
					{
						setName("a");
					}

				}));

			}
		}.setTableMetaData(new TableMetaData() {
			{
				setName("tb_name");
				setColumns(Arrays.asList(//
						new ColumnMetaData() {
					{
						setName("a");
					}

				}, new ColumnMetaData() {
					{
						setName("c");
					}

				}, new ColumnMetaData() {
					{
						setName("b");
					}

				}));
			}
		})));
	}

	@Test
	public void testGenDelete() {
		System.out.println(genDalete(new SqlExcutorContext() {
			{
				setConditionColumnMetaData(Arrays.asList(new ColumnMetaData() {
					{
						setName("a");
					}

				}));

			}
		}.setTableMetaData(new TableMetaData() {
			{
				setName("tb_name");
				setColumns(Arrays.asList(//
						new ColumnMetaData() {
					{
						setName("a");
					}

				}, new ColumnMetaData() {
					{
						setName("c");
					}

				}, new ColumnMetaData() {
					{
						setName("b");
					}

				}));
			}
		})));
	}

	@Test
	public void testGenSelect() {
		System.out.println(genSelect(new SqlExcutorContext() {
			{
				setConditionColumnMetaData(Arrays.asList(new ColumnMetaData() {
					{
						setName("a");
					}

				}));

			}
		}.setTableMetaData(new TableMetaData() {
			{
				setName("tb_name");
				setColumns(Arrays.asList(//
						new ColumnMetaData() {
					{
						setName("a");
					}

				}, new ColumnMetaData() {
					{
						setName("c");
					}

				}, new ColumnMetaData() {
					{
						setName("b");
					}

				}));
			}
		})));
	}

}
